@echo off
chcp 65001 >nul
cls
echo Feifan Task Manager
echo ===================

:menu
echo.
echo Please select an option:
echo 1. Setup scheduled tasks
echo 2. Start Feifan system
echo 3. Stop Feifan system
echo 4. Execute daily task
echo 5. View task status
echo 6. Check service status
echo 7. Delete all tasks
echo 0. Exit
echo.
set /p choice=Enter your choice (0-7): 

if "%choice%"=="1" goto setup
if "%choice%"=="2" goto start
if "%choice%"=="3" goto stop
if "%choice%"=="4" goto daily
if "%choice%"=="5" goto status
if "%choice%"=="6" goto check
if "%choice%"=="7" goto delete
if "%choice%"=="0" goto exit
echo Invalid choice, please try again
goto menu

:setup
echo Setting up scheduled tasks...
call "bat\setup-scheduler.bat"
goto menu

:start
echo Starting Feifan system...
call "bat\start-feifan.bat"
goto menu

:stop
echo Stopping Feifan system...
call "bat\stop-feifan.bat"
goto menu

:daily
echo Executing daily task...
call "bat\daily-task.bat"
goto menu

:status
echo Viewing task status...
echo.
echo === Feifan Daily Task ===
schtasks /query /tn "飞帆每日任务" 2>nul
echo.
echo === Feifan System Start ===
schtasks /query /tn "飞帆系统启动" 2>nul
echo.
echo === Feifan Boot Task ===
schtasks /query /tn "飞帆开机任务" 2>nul
echo.
pause
goto menu

:check
echo Checking service status...
echo.
echo === PM2 Process Status ===
pm2 list | findstr "feifan-api"
echo.
echo === Port Usage ===
netstat -an | findstr ":3333"
echo.
echo === Service Connection Test ===
curl -s http://localhost:3333/get-token >nul 2>&1
if errorlevel 1 (
    echo Service connection failed: Service not running or inaccessible
) else (
    echo Service connection successful: Service is running
)
echo.
pause
goto menu

:delete
echo Deleting all tasks...
schtasks /delete /tn "飞帆每日任务" /f 2>nul
schtasks /delete /tn "飞帆系统启动" /f 2>nul
schtasks /delete /tn "飞帆开机任务" /f 2>nul
echo All tasks deleted
pause
goto menu

:exit
echo Goodbye!
exit /b 0
